// import {localGet, localSet} from "@/common/utils/GlobalUtils";
import {localGet,localSet} from "@/common/utils";

export const useSetFormData = (config={key:''}) => {
    let UserId = localStorage.getItem('SST-UserId');
    config.key = `${UserId.replace(new RegExp('"','g'),"")}-${config.key}`;

    const getData = (key) => {
        return localGet(key===undefined?config.key:key);
    }

    /**
     * 筛选掉对象的空属性
     */
    const filterEmptyAttributes = (data) => {
        if(!data) data = {};
        data = JSON.parse(JSON.stringify(data))
        let newObj = {};
        for (let key in data){
            if(Array.isArray(data[key]) && data[key] && !data[key][0]){
                delete data[key];
            }
            if(data[key]){
                newObj[key] = data[key];
            }
        }
        return newObj;
    }

    /**
     * 将数据写入缓存
     * @param data
     * @param key
     */
    const setData = (data,key) => {
        console.log('setData',data);
        localSet(key===undefined?config.key:key,JSON.parse(JSON.stringify(filterEmptyAttributes(data))));
    }

    /**
     * 获取对象属性的数量
     */
    const getAttributeNum = (data) => {
        let obj = filterEmptyAttributes(data);
        let count = 0
        for(let key in obj) {
            count++;
        }
        return count;
    }

    return {
        setData,
        getData,
        filterEmptyAttributes,
        getAttributeNum
    }
}
